package com.bjfu.gmms.servlet;


import com.bjfu.gmms.MyBatisUtil;
import com.bjfu.gmms.mapper.StudentInfoMapper;
import com.bjfu.gmms.mapper.StudentMapper3;
import com.bjfu.gmms.model.Student;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/studentInfoQuery")
public class StudentInfoQueryServlet extends HttpServlet {
    private StudentMapper3 studentMapper;
    private StudentInfoMapper studentInfoMapper; // add 用来查询学科名称
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String studentId = request.getParameter("studentId");
        SqlSession sqlSession = MyBatisUtil.getSqlSession("development");
        studentMapper = sqlSession.getMapper(StudentMapper3.class);

        // 获取学生的完整信息（包括成绩）
        List<Student> studentList = studentMapper.getStudentFullInfo(Integer.parseInt(studentId));

        if (studentList != null && !studentList.isEmpty()) {
            // 将学生的成绩信息传递给 JSP
            Student student = studentList.get(0); // 只取第一个学生（因为返回的是同一名学生的不同科目）
            request.setAttribute("student", student);
            request.setAttribute("studentScores", studentList); // 将所有成绩数据传递给 JSP

            //add
            // 改：根据学科名称查询学科 ID（字符串类型）
            studentInfoMapper = sqlSession.getMapper(StudentInfoMapper.class);
            String DisciplineName = studentInfoMapper.getDisciplineNameById(student.getSecondaryDisciplineId());
            System.out.println(DisciplineName);
            request.setAttribute("disciplineName", DisciplineName); // 将被录取的考试科目数据传递给 JSP
            if (DisciplineName == null) {
                throw new ServletException("无法找到对应的学科ID");
            }

            RequestDispatcher dispatcher = request.getRequestDispatcher("studentInfoResult.jsp");

            dispatcher.forward(request, response);
        } else {
            response.getWriter().write("考生ID不存在");
        }
    }
}

